package com.soikc.kuaidiE.service;

import com.alibaba.fastjson.JSONObject;
import com.soikc.kuaidiE.bean.KuaiDi;
import com.soikc.kuaidiE.dao.imp.KuaiDiDaoImp;
import com.soikc.kuaidiE.util.RandomUtil;

import java.util.Date;
import java.util.List;

public class KuaiDiService {
    private static KuaiDiDaoImp dao = new KuaiDiDaoImp();
    /**
     * 获取所有快递
     * @return
     */
    public List<KuaiDi> queryList() {
        return dao.queryList();
    }

    /**
     * 分页查询
     * @param offset     开始位置
     * @param pageNumber 查询数量
     * @return
     */
    public List<KuaiDi> queryList(int offset, int pageNumber) {
        return dao.queryList(offset,pageNumber);
    }

    /**
     * 添加一个快递
     * @param kuaiDi
     * @return
     */
    public boolean add(KuaiDi kuaiDi) {
        String s;
        do {
            //生成取件码，重复则重新生成
            s = RandomUtil.get() + "";
        }while (dao.queryPickup_code(s)!=null);
        kuaiDi.setPickup_code(s);
        kuaiDi.setDeposit_time(new Date());
        return dao.add(kuaiDi);
    }
    /**
     * 修改一个快递
     * @param id
     * @param kuaiDi
     * @return
     */
    public boolean update(int id, KuaiDi kuaiDi) {
       if (kuaiDi.getStatus()==1){
           //如果状态变为已取件则更新取件时间
           kuaiDi.setTake_out_time(new Date());
       }
        return dao.update(id,kuaiDi);
    }
    /**
     * 以单号查询
     * @param single_number 快递单号
     * @return
     */
    public KuaiDi querySingle_number(String single_number) {
        return dao.querySingle_number(single_number);
    }

    /**
     * 以取件码查询
     * @param code
     * @return
     */
    public KuaiDi queryPickup_code(String code) {
        return dao.queryPickup_code(code);
    }

    /**
     * 以手机号查询
     * @param phone
     * @return
     */
    public List<KuaiDi> queryPhone(String phone) {
        return dao.queryPhone(phone);
    }

    /**
     * 以存入人手机号查询
     * @param phone
     * @return
     */
    public List<KuaiDi> queryDepositor_phone(String phone) {
        return dao.queryDepositor_phone(phone);
    }
    /**
     * 以id删除快递
     * @param id
     * @return
     */
    public boolean delID(int id) {
        return dao.delID(id);
    }

    public JSONObject queryConsole() {
        return dao.queryConsole();
    }
    /**
     * 以单号修改快递状态
     * @param id
     * @param status
     * @return
     */
    public boolean updateStatus(int id, int status) {
        KuaiDi kuaiDi = new KuaiDi();
        kuaiDi.setId(id);
        kuaiDi.setStatus(status);
        return update(id,kuaiDi);
    }
}
